package com.woniuxy.carlife.carlifeuserweb.controller;


import cn.hutool.core.util.ObjectUtil;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.carlifecommonsweb.util.CopyUtil;
import com.woniuxy.carlife.carlifecommonsweb.util.JwtUtil;
import com.woniuxy.carlife.carlifeuserclient.dto.ChargingStationDto;
import com.woniuxy.carlife.carlifeuserclient.dto.ProductCollectDto;
import com.woniuxy.carlife.carlifeuserclient.dto.ServiceInfoDto;
import com.woniuxy.carlife.carlifeuserclient.dto.StoreinfoDto;
import com.woniuxy.carlife.carlifeuserclient.otherFeign.ChargingFegin;
import com.woniuxy.carlife.carlifeuserclient.otherFeign.ProductFeign;
import com.woniuxy.carlife.carlifeuserclient.otherFeign.ServiceInfofavoritesFegin;
import com.woniuxy.carlife.carlifeuserclient.otherFeign.StorefavoritesFeign;
import com.woniuxy.carlife.carlifeuserclient.param.ProductFindParam;
import com.woniuxy.carlife.carlifeusermodel.dto.*;
import com.woniuxy.carlife.carlifeusermodel.param.*;
import com.woniuxy.carlife.carlifeuserweb.entity.*;
import com.woniuxy.carlife.carlifeuserweb.service.ChargingStationfavoritesService;
import com.woniuxy.carlife.carlifeuserweb.service.ProdectfavoritesService;
import com.woniuxy.carlife.carlifeuserweb.service.ServiceInfofavoritesService;
import com.woniuxy.carlife.carlifeuserweb.service.StorefavoritesService;
import com.woniuxy.carlife.carlifeuserweb.util.StatusMessageEnum;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zjq
 * @ClassName:c
 * @Description:〈根据用户id查询收藏的商品〉
 * @date 2021/4/6 20:02
 **/

@RestController
@Slf4j
@Api(tags = "收藏管理接口信息")//用于描述接口类的相关信息，作用于类上

@RequestMapping("/prodectfavorites")
public class ProdectfavoritesController {

    @Resource
    ProdectfavoritesService prodectfavoritesService;

    //    充电站收藏
    @Resource
    ChargingStationfavoritesService chargingStationfavoritesService;

    //门店项目服务收藏
    @Resource
    ServiceInfofavoritesService serviceInfofavoritesService;

    //    门店收藏
    @Resource
    StorefavoritesService storefavoritesService;

    @Resource
    ServiceInfofavoritesFegin serviceInfofavoritesFegin;

    @Resource
    ProductFeign productFeign;

    @Resource
    ChargingFegin chargingFegin;

    @Resource
    StorefavoritesFeign storefavoritesFeign;

    /*
     * 功能描述〈根据用户id查询收藏的商品〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 13:16
     * @throws
     */
    @ApiOperation(value = "根据用户id查询收藏的商品，获取token")

    @GetMapping("/ordinary/findProdectfavoritesByUserId")
    public Result findProdectfavoritesByUserId() {
        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            return new Result(false, StatusCode.NOT_LOGGED_IN, StatusMessageEnum.NOT_LOGGED_IN.toString());
        }
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));

        List<Prodectfavorites> prodectfavoritesByUserId = prodectfavoritesService.findProdectfavoritesByUserId(tokenUserId);


        if (ObjectUtil.isNull(prodectfavoritesByUserId)) {
            return new Result(false, StatusCode.ERROR, StatusMessageEnum.QUERY_IS_EMPTY.toString());
        }

        List<ProdectfavoritesDto> prodectfavoritesList = CopyUtil.getList(prodectfavoritesByUserId, ProdectfavoritesDto.class);

        return new Result(true, StatusCode.SEARCH_SUCCESSFUL, StatusMessageEnum.SEARCH_SUCCESSFUL.toString(), prodectfavoritesList);

    }


    /*
     * 功能描述〈查询所有收藏〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 14:44
     * @throws
     */
    @ApiOperation(value = "查询所有收藏，获取token")

    @GetMapping("/ordinary/findFavoritesByUserId")
    public Result findFavoritesByUserId() {
        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            return new Result(false, StatusCode.NOT_LOGGED_IN, StatusMessageEnum.NOT_LOGGED_IN.toString());
        }
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));

        List<FavoritesDto> favoritesByUserId = prodectfavoritesService.findFavoritesByUserId(tokenUserId);


        if (ObjectUtil.isNull(favoritesByUserId)) {
            return new Result(false, StatusCode.QUERY_IS_EMPTY, StatusMessageEnum.QUERY_IS_EMPTY.toString());
        }


        return new Result(true, StatusCode.SEARCH_SUCCESSFUL, StatusMessageEnum.SEARCH_SUCCESSFUL.toString(), favoritesByUserId);
    }



    /*
     * 功能描述〈用户删除收藏〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/14 15:07
     * @throws
     */

    @ApiOperation(value = "用户删除收藏(删除商品、门店、门店服务、充电站)")
    @PostMapping("/ordinary/deleteFavorites")
    public Result deleteFavorites(@ApiParam FavoritesParam favoritesParam) {

        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            return new Result(false, StatusCode.NOT_LOGGED_IN, StatusMessageEnum.NOT_LOGGED_IN.toString());
        }
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));

        if (ObjectUtil.isNull(favoritesParam)) {
            return new Result(false, StatusCode.CAN_NOT_BE_EMPTY, StatusMessageEnum.CAN_NOT_BE_EMPTY.toString());
        }
        if (ObjectUtil.isNull(favoritesParam.getProductfavoritesId()) && ObjectUtil.isNull(favoritesParam.getStorefavoritesId())
                && ObjectUtil.isNull(favoritesParam.getTChargingStationfavoritesId()) && ObjectUtil.isNull(favoritesParam.getTServiceInfofavoritesId())) {
            return new Result(false, StatusCode.FAILED_TO_DELETE, StatusMessageEnum.FAILED_TO_DELETE.toString());

        }
        log.info(tokenUserId + "开始删除");
        boolean b = prodectfavoritesService.deleteFavorites(favoritesParam, tokenUserId);
        if (!b) {
            return new Result(false, StatusCode.FAILED_TO_DELETE, StatusMessageEnum.FAILED_TO_DELETE.toString());

        }
        log.info(tokenUserId + "删除成功");

        return new Result(true, StatusCode.SUCCESSFULLY_DELETED, StatusMessageEnum.SUCCESSFULLY_DELETED.toString());

    }


    /*
     * 功能描述〈据用户id新增收藏的商品〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/14 18:43
     * @throws
     */
    @ApiOperation(value = "用户增收藏的商品")
    @PostMapping("/ordinary/addProdectfavoritesByUserid")
    public Result addProdectfavoritesByUserid(Integer productId) {

        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            return new Result(false, StatusCode.NOT_LOGGED_IN, StatusMessageEnum.NOT_LOGGED_IN.toString());
        }
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));

        if (ObjectUtil.isNull(productId)) {
            return new Result(false, StatusCode.CAN_NOT_BE_EMPTY, StatusMessageEnum.CAN_NOT_BE_EMPTY.toString());

        }

        ProductCollectDto productCollectById = productFeign.findProductCollectById(productId);
        Prodectfavorites one = CopyUtil.getOne(productCollectById, Prodectfavorites.class);
        one.setUserId(tokenUserId);
        Boolean aBoolean = prodectfavoritesService.addProdectfavoritesByUserid(one);
        return aBoolean ?
                new Result(true, StatusCode.ADDED_SUCCESSFULLY, StatusMessageEnum.ADDED_SUCCESSFULLY.toString())
                : new Result(false, StatusCode.ADD_FAILED, StatusMessageEnum.ADD_FAILED.toString());


    }




    /*
     * 功能描述〈查询充电站收藏根据用户id〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 14:34
     * @throws
     */

    @ApiOperation(value = "查询充电站收藏")
    @GetMapping("/ordinary/findChargingStationfavoritesByUserId")
    public Result findChargingStationfavoritesByUserId() {
        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            return new Result(false, StatusCode.NOT_LOGGED_IN, StatusMessageEnum.NOT_LOGGED_IN.toString());
        }
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));
        List<ChargingStationfavorites> chargingStationfavoritesByUserId = chargingStationfavoritesService.findChargingStationfavoritesByUserId(tokenUserId);


        if (ObjectUtil.isNull(chargingStationfavoritesByUserId)) {
            return new Result(false, StatusCode.QUERY_IS_EMPTY, StatusMessageEnum.QUERY_IS_EMPTY.toString());
        }

        List<ChargingStationfavoritesDto> chargingStationfavoritesList = CopyUtil.getList(chargingStationfavoritesByUserId, ChargingStationfavoritesDto.class);

        return new Result(true, StatusCode.SEARCH_SUCCESSFUL, StatusMessageEnum.SEARCH_SUCCESSFUL.toString(), chargingStationfavoritesList);
    }

    /*
     * 功能描述〈新增充电站收藏〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/14 20:03
     * @throws
     */
    @ApiOperation(value = "新增充电站收藏")
    @PostMapping("/ordinary/addChargingStationfavoritesBytChargingStationfavoritesId")
    public Result addChargingStationfavoritesBytChargingStationfavoritesId( Integer chargingStationId) {
        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            return new Result(false, StatusCode.NOT_LOGGED_IN, StatusMessageEnum.NOT_LOGGED_IN.toString());
        }
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));
        if (chargingStationId==null && chargingStationId==0) {
            return new Result(false, StatusCode.CAN_NOT_BE_EMPTY, StatusMessageEnum.CAN_NOT_BE_EMPTY.toString());

        }
        ChargingStationDto byOne = chargingFegin.findByOne(chargingStationId);

        ChargingStationfavorites one = CopyUtil.getOne(byOne, ChargingStationfavorites.class);

        one.setUserId(tokenUserId);
        Boolean aBoolean = chargingStationfavoritesService.addChargingStationfavoritesBytChargingStationfavoritesId(one);
        return aBoolean ?
                new Result(true, StatusCode.ADDED_SUCCESSFULLY, StatusMessageEnum.ADDED_SUCCESSFULLY.toString())
                : new Result(false, StatusCode.ADD_FAILED, StatusMessageEnum.ADD_FAILED.toString());


    }


    /*
     * 功能描述〈查询门店项目服务收藏根据用户id〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 14:24
     * @throws
     */
    @ApiOperation(value = "查询门店项目服务收藏")
    @GetMapping("/ordinary/findServiceInfofavoritesByUserId")
    public Result findServiceInfofavoritesByUserId() {
        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            return new Result(false, StatusCode.ERROR, StatusMessageEnum.NOT_LOGGED_IN.toString());
        }
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));
        List<ServiceInfofavorites> serviceInfofavoritesByUserId = serviceInfofavoritesService.findServiceInfofavoritesByUserId(tokenUserId);


        if (ObjectUtil.isNull(serviceInfofavoritesByUserId)) {
            return new Result(false, StatusCode.ERROR, StatusMessageEnum.QUERY_IS_EMPTY.toString());
        }

        List<ServiceInfofavoritesDto> serviceInfofavoritesList = CopyUtil.getList(serviceInfofavoritesByUserId, ServiceInfofavoritesDto.class);

        return new Result(true, StatusCode.OK, StatusMessageEnum.SEARCH_SUCCESSFUL.toString(), serviceInfofavoritesList);
    }

    /*
     * 功能描述〈门店服务收藏新增〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/14 19:18
     * @throws
     */
    @ApiOperation(value = "门店服务收藏新增")
    @GetMapping("/ordinary/addServiceInfofavoritesByUserId")
    public Result addServiceInfofavoritesByUserId(Integer infoId) {

        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            return new Result(false, StatusCode.ERROR, StatusMessageEnum.NOT_LOGGED_IN.toString());
        }
        if (infoId == 0 && infoId < 0) {
            return new Result(false, StatusCode.CAN_NOT_BE_EMPTY, StatusMessageEnum.CAN_NOT_BE_EMPTY.toString());

        }
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));
//        新增门店服务id，使用fegin查询后加入
        ServiceInfoDto servcieInfoByServiceInfoId1 = serviceInfofavoritesFegin.findServcieInfoByServiceInfoId(infoId);

        ServiceInfofavorites one = CopyUtil.getOne(servcieInfoByServiceInfoId1, ServiceInfofavorites.class);
        one.setUserId(tokenUserId);
        Boolean aBoolean = serviceInfofavoritesService.addServiceInfofavoritesByUserId(one);

        return aBoolean ?
                new Result(true, StatusCode.ADDED_SUCCESSFULLY, StatusMessageEnum.ADDED_SUCCESSFULLY.toString())
                : new Result(false, StatusCode.ADD_FAILED, StatusMessageEnum.ADD_FAILED.toString());


    }


    /*
     * 功能描述〈查询门店收藏，根据用户id〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 14:14
     * @throws
     */

    @ApiOperation(value = "查询门店收藏")
    @GetMapping("/ordinary/findStorefavorites")
    public Result findStorefavorites() {
        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            return new Result(false, StatusCode.ERROR, StatusMessageEnum.NOT_LOGGED_IN.toString());
        }
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));


        List<Storefavorites> storefavorites = storefavoritesService.findStorefavorites(tokenUserId);


        if (ObjectUtil.isNull(storefavorites)) {
            return new Result(false, StatusCode.ERROR, StatusMessageEnum.QUERY_IS_EMPTY.toString());
        }

        List<StorefavoritesDto> storefavoritesList = CopyUtil.getList(storefavorites, StorefavoritesDto.class);

        return new Result(true, StatusCode.OK, StatusMessageEnum.SEARCH_SUCCESSFUL.toString(), storefavoritesList);
    }


    /*
     * 功能描述〈门店的新增〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/14 19:48
     * @throws
     */
    @ApiOperation(value = "门店收藏的新增")
    @GetMapping("/ordinary/addStorefavoritesByUserId")
    public Result addStorefavoritesByUserId(Integer storeId) {
        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            return new Result(false, StatusCode.NOT_LOGGED_IN, StatusMessageEnum.NOT_LOGGED_IN.toString());
        }
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));
        if (storeId ==0 && storeId==null) {
            return new Result(false, StatusCode.CAN_NOT_BE_EMPTY, StatusMessageEnum.CAN_NOT_BE_EMPTY.toString());

        }
        StoreinfoDto storeList = storefavoritesFeign.getStoreList(storeId);
        Storefavorites one = CopyUtil.getOne(storeList, Storefavorites.class);
        one.setUserId(tokenUserId);
        Boolean aBoolean = storefavoritesService.addStorefavoritesByUserId(one);
        return aBoolean ?
                new Result(true, StatusCode.ADDED_SUCCESSFULLY, StatusMessageEnum.ADDED_SUCCESSFULLY.toString())
                : new Result(false, StatusCode.ADD_FAILED, StatusMessageEnum.ADD_FAILED.toString());


    }


}

